<?php
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../DAO/ScheduleDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../DAO/MemberDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Util/Tool.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Info/Schedule.php';
class ScheduleService
{

	public function createSchedule(BaseContext $context, ServiceResult $result,ScheduleDomain $scheduleDomain) {
		try 
		{
			$scheduleDao = new ScheduleDAO($context);
			$scheduleDao->create($scheduleDomain);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
	
	public function updateSchedule(BaseContext $context, ServiceResult $result,ScheduleDomain $scheduleDomain) {
		try 
		{
			$scheduleDao = new ScheduleDAO($context);
			$scheduleDao->update($scheduleDomain);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
	
	public function deleteSchedule(BaseContext $context, ServiceResult $result,$scheduleID) {
		try 
		{
			$scheduleDao = new ScheduleDAO($context);
			$scheduleDao->deleteByOid($scheduleID);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
	
	public function getScheduleByStatusByDeliveryDate(BaseContext $context, ServiceResult $result,$status, $dateNow, $timeNow, $start, $limit)
	{
		$scheduleArr = array();
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomainArr = $scheduleDao->findScheduleByStatusByDeliveryDate($status, $dateNow, $timeNow, $start, $limit);
			if(count($scheduleDomainArr)!=0)
			{
				foreach ($scheduleDomainArr as $scheduleDomain)
				{
					$schedule = new Schedule($scheduleDomain);
					$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
					if($memberDomain != null)
					{
						$schedule->setMemberDomain($memberDomain);
						$scheduleArr[] = $schedule;
					}
				}
			}
			$result->setSuccess();
			return $scheduleArr;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
		
	}
	
	public function countScheduleByStatusByDeliveryDate(BaseContext $context, ServiceResult $result,$status, $dateNow, $timeNow)
	{
		$count= 0;
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$count = $scheduleDao->countScheduleByStatusByDeliveryDate($status, $dateNow, $timeNow);
			$result->setSuccess();							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
		return $count;
		
	}
	
	public function getScheduleByStatusByCollectionDate(BaseContext $context, ServiceResult $result,$status, $dateNow, $timeNow, $start, $limit)
	{
		$scheduleArr = array();
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomainArr = $scheduleDao->findScheduleByStatusByCollectionDate($status, $dateNow, $timeNow, $start, $limit);
			if(count($scheduleDomainArr)!=0)
			{
				foreach ($scheduleDomainArr as $scheduleDomain)
				{
					$schedule = new Schedule($scheduleDomain);
					$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
					if($memberDomain!= null)
					{
						$schedule->setMemberDomain($memberDomain);
						$scheduleArr[] = $schedule;
					}
				}
			}
			$result->setSuccess();
			return $scheduleArr;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
		
	}
	
	public function countScheduleByStatusByCollectionDate(BaseContext $context, ServiceResult $result,$status, $dateNow, $timeNow)
	{
		$count= 0;
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$count = $scheduleDao->countScheduleByStatusByCollectionDate($status, $dateNow, $timeNow);
			$result->setSuccess();							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
		return $count;
		
	}
	
	public function getAllSchedule (BaseContext $context, ServiceResult $result)
	{
		$scheduleArr = array();
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomainArr = $scheduleDao->findAllSchedule();
			foreach ($scheduleDomainArr as $scheduleDomain)
			{
				$schedule = new Schedule($scheduleDomain);
				$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
				if($memberDomain!= null)
				{
					$schedule->setMemberDomain($memberDomain);
					$scheduleArr[] = $schedule;
				}
			}
			$result->setSuccess();
			return $scheduleArr;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
		
	}
	
	public function getScheduleByOid (BaseContext $context, ServiceResult $result, $id)
	{
		$schedule = null;
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomain = $scheduleDao->findByOid($id);			
			$schedule = new Schedule($scheduleDomain);
			$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
			if($memberDomain!= null)
			{
				$schedule->setMemberDomain($memberDomain);
			}
			$result->setSuccess();
			return $schedule;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
		
	}
	
	public function getFinishedSchedule(BaseContext $context, ServiceResult $result,$status)
	{
		$scheduleArr = array();
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomainArr = $scheduleDao->findScheduleByStatus($status);
			foreach ($scheduleDomainArr as $scheduleDomain)
			{
				$schedule = new Schedule($scheduleDomain);
				$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
				if($memberDomain!= null)
				{
					$schedule->setMemberDomain($memberDomain);
					$scheduleArr[] = $schedule;
				}
			}
			$result->setSuccess();
			return $scheduleArr;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
	}
	
	public function getNewSchedule(BaseContext $context, ServiceResult $result)
	{
		$scheduleArr = array();
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomainArr1 = $scheduleDao->findScheduleByStatus(0);
			$scheduleDomainArr2 = $scheduleDao->findScheduleByStatus(1);
			$scheduleDomainArr = array_merge($scheduleDomainArr2,$scheduleDomainArr1);
			foreach ($scheduleDomainArr as $scheduleDomain)
			{
				$schedule = new Schedule($scheduleDomain);
				$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
				if($memberDomain!= null)
				{
					$schedule->setMemberDomain($memberDomain);
					$scheduleArr[] = $schedule;
				}
			}
			$result->setSuccess();
			return $scheduleArr;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
	}
	
	public function getScheduleByStatus(BaseContext $context, ServiceResult $result,$status)
	{
		$scheduleArr = array();
		try 
		{			
			$scheduleDao = new ScheduleDAO($context);
			$memberDao = new MemberDAO($context);
			$scheduleDomainArr = $scheduleDao->findScheduleByStatus($status);
			foreach ($scheduleDomainArr as $scheduleDomain)
			{
				$schedule = new Schedule($scheduleDomain);
				$memberDomain = $memberDao->findByDwoNumber($scheduleDomain->getDwoNumber());
				if($memberDomain!= null)
				{
					$schedule->setMemberDomain($memberDomain);
					$scheduleArr[] = $schedule;
				}
			}
			$result->setSuccess();
			return $scheduleArr;							
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return null;
		}
	}
	
	public function updateIsSent(BaseContext $context, ServiceResult $result, $scheduleID, $isSent)
	{
		try 
		{
			$scheduleDao = new ScheduleDAO($context);
			$scheduleDao->updateIsSent($scheduleID, $isSent);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
}